package com.myph.chart;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.myph.model.Util;
import com.myph.phctrl_t.MainActivity;

public class MyView extends View {
    private int stX;
    private int stY;
    private int XScale;
    private int YScale;
    private int YNum;
    private int XLength = 380;
    private int YLength = 240;
    private int allDataSize = 7;
    private int curNo = 0;
    private int MaxDataSize = 5;
    private double[] sensorData;
    private List<Integer> data = new ArrayList<Integer>();
    private String[] YLabel = new String[16];
    private int[] sdPos;
    private Handler nextHandler;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0x1234) {
                MyView.this.invalidate();
            }
        }

        ;
    };

    //    public MyView(Context context, AttributeSet attrs) {
    public MyView(Context context, double[] res,Handler hand) throws InterruptedException {
        super(context);
        sensorData = res;
        nextHandler = hand;
        
        init();
//        System.out.println("after init");
//        for(int i=0;i<MaxDataSize;++i){
//            data.add(new Random().nextInt(10));
//
//        }
        if (res == null) {
            Toast.makeText(getContext(), "null",
                    Toast.LENGTH_SHORT).show();
            Thread.sleep(3);
            return;
        }
        for (int i = 0; i < res.length; ++i) {
            Toast.makeText(getContext(), Double.toString((double) res[i]),
                    Toast.LENGTH_SHORT).show();
        }
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (data.size() >= MaxDataSize) {
                        data.remove(0);
                    }
                    if (curNo >= allDataSize) {
                        break;
                    }
                    data.add(sdPos[curNo]);
                    ++curNo;
                    System.out.println("curNo:"+curNo);
                    handler.sendEmptyMessage(0x1234);
                }
                try {
//                    Toast.makeText(getContext(), "return ", Toast.LENGTH_SHORT).show();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                nextHandler.sendEmptyMessage(4);
                
            }
        }).start();
        Toast.makeText(getContext(), "end",
                Toast.LENGTH_SHORT).show();
    }

    private void chDoubleToPos() {
        sdPos = new int[sensorData.length];
        System.out.println("sensorDataPos:");
//        double posScale = YLength * 4.0 / 5.0 / 15.0;
        for (int i = 0; i < sensorData.length; ++i) {
            sdPos[i] = (int) (sensorData[i] * 1.0*YScale);
            System.out.println(sensorData[i]+","+sdPos[i] + ",");
        }

    }

    private void init() {
        allDataSize = sensorData.length;
        XLength = MainActivity.screenw;
        YLength = MainActivity.screenh;
        Log.d(Util.TAG,"MyView:X,YLength:"+XLength+","+YLength);
        for (int i = 0; i <= 14; ++i) {
            YLabel[i] = Integer.toString(i) + ".0";
        }
        YLabel[15] = "";
        MaxDataSize = 5;
        XScale = (XLength * 7 / 8) / MaxDataSize;
        YNum = 15;
        YScale = (YLength * 4 / 5) / (YNum);
        stX = XLength / 8;
        stY = 0;
        System.out.println("x,y:" + XLength + YLength);
        chDoubleToPos();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //init
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);//white
        paint.setTextSize(30);
        canvas.drawColor(Color.WHITE);
        
        //draw the x,y coordinate
        canvas.drawLine(stX, stY, stX, YLength * 8 / 10, paint);
        canvas.drawLine(stX, stY, stX - XLength / 32, YLength / 32, paint);  
        canvas.drawLine(stX, stY, stX + XLength / 32, YLength / 32, paint);
        System.out.println("stX,stY:"+stX+","+stY);
        //draw the arrow
        for (int i = 1; i <= YNum; ++i) {

            canvas.drawLine(stX, stY + YLength * 4 / 5 * i / (YNum), stX + XLength / 14, stY + YLength * 4 / 5 * i / (YNum), paint);  //�̶�

            canvas.drawText(YLabel[YNum - i], 1, stY + YLength * 4 / 5 * i / (YNum), paint);
        }

        //draw the line
        canvas.drawLine(stX, YLength * 4 / 5, XLength, YLength * 4 / 5, paint);
        System.out.println("Data.size = " + data.size());
        if (data.size() > 1) {
            for (int i = 1; i < data.size(); i++) {
//                canvas.drawLine(stX + (i-1) * XScale, YLength*4/5 - data.get(i-1) * YScale,
//                        stX + i * XScale, YLength*4/5 - data.get(i) * YScale, paint);
                canvas.drawLine(stX + (i - 1) * XScale, YLength*4/5 - data.get(i - 1), stX + i * XScale, YLength*4/5 -data.get(i), paint);
            }
        }
    }
}  
